<?php
namespace Backend\Controllers\Setting\My;

use Backend\Controllers\BaseController;
use Backend\Models\Sys\User;

class ProfileController extends BaseController
{

    public function initialize()
    {
        parent::initialize();
    }

    public function getAction()
    {

    }

    /**
     * 重置用户信息功能
     *
     * 路由支持
     * /my/profile/reset/password/
     * /my/profile/reset/profile/
     */
    public function resetAction()
    {
        $params = $this->dispatcher->getParams();
        if (is_array($params) && count($params) > 0 && isset($params[0])) {
            $param = $params[0];
            if ($param == 'password') {
                return $this->password();
            } else {
                if ($param == 'profile') {
                    return $this->profile();
                }
            }
        }
        return $this->json([
            'code' => 404,
            'msg'  => '没有此功能'
        ]);
    }

    public function resetPasswordAction()
    {
        $user = $this->user;
        if ($user == false) {
            return $this->json([
                'code' => 500,
                'msg'  => '请您先登录～'
            ]);
        }
        $data = [
            'name'          => $this->request->getPost('name'),
            'password'      => $this->request->getPost('password'),
            'new_password'  => $this->request->getPost('new_password'),
            'new_password2' => $this->request->getPost('new_password2')
        ];
        if ($data['password'] == '') {
            return $this->json([
                'code' => 301,
                'msg'  => '旧密码不能为空～'
            ]);
        }
        if ($data['new_password'] == '') {
            return $this->json([
                'code' => 302,
                'msg'  => '新密码不能为空～'
            ]);
        }
        if (mb_strlen($data['new_password'], 'utf-8') < 6) {
            return $this->json([
                'code' => 303,
                'msg'  => '新密码长度不能小于6位～'
            ]);
        }
        if (mb_strlen($data['new_password'], 'utf-8') != mb_strlen(trim($data['new_password']), 'utf-8')) {
            return $this->json([
                'code' => 303,
                'msg'  => '新密码两端不能出现空格～'
            ]);
        }
        if ($data['new_password'] != $data['new_password2']) {
            return $this->json([
                'code' => 305,
                'msg'  => '新密码不一致，请重新输入～'
            ]);
        }
        $uid = $user->uid;
        if ($uid !== false) {
            $userModel = new User();
            $result    = $userModel->getUserInfoByUid($uid);
            if ($result['password'] != md5($data['password'])) {
                return $this->json([
                    'code' => 306,
                    'msg'  => '旧密码不正确～'
                ]);
            }
            $entity = [
                'uid'      => $result['uid'],
                'password' => md5($data['new_password'])
            ];
            if ($userModel->resetPassword($entity)) {
                return $this->json(['code' => 200, 'msg' => '密码修改成功～']);
            }
            return $this->json(['code' => 307, 'msg' => '密码修改失败～']);
        }
        return $this->json(['code' => 308, 'msg' => '系统错误～']);
    }

    public function profileAction()
    {
        $user = $this->user;
        if ($user == false) {
            return $this->json([
                'code' => 500,
                'msg'  => '请您先登录～'
            ]);
        }
        $result = (new User())->getUserInfoByUid($user->uid);
        if ($result === false) {
            return $this->json([
                'code' => 301,
                'msg'  => '获取信息失败,请稍候再试～'
            ]);
        }
        if ($result !== false && isset($result['uid'])) {
            unset($result['password']);
            unset($result['role_id']);
            return $this->json([
                'code' => 200,
                'user' => $result
            ]);
        }
    }

    public function resetProfileAction()
    {
        if ($this->user == false) {
            return $this->json([
                'code' => 500,
                'msg'  => '请您先登录～'
            ]);
        }
        $data = [
            'name'   => $this->request->getPost('name'),
            'mobile' => $this->request->getPost('mobile'),
            'tel'    => $this->request->getPost('tel'),
            'email'  => $this->request->getPost('email')
        ];
        if ($data['name'] == '') {
            return $this->json([
                'code' => 301,
                'msg'  => '请填写姓名～'
            ]);
        }
        if (mb_strlen($data['name'], 'utf-8') > 10) {
            return $this->json([
                'code' => 302,
                'msg'  => '姓名太长，超过10个字～'
            ]);
        }
        $uid = $this->user->uid;
        if ($uid !== false) {
            $user         = new User();
            $entity       = $user->getUserInfoByUid($uid);
            $updateEntity = [
                'uid'    => $entity['uid'],
                'name'   => $data['name'],
                'email'  => $data['email'],
                'tel'    => $data['tel'],
                'mobile' => $data['mobile']
            ];
            if ($user->edit($updateEntity)) {
                return $this->json([
                    'code' => 200,
                    'msg'  => '修改成功～'
                ]);
            }
            return $this->json([
                'code' => 301,
                'msg'  => '修改资料失败～'
            ]);
        }
        return $this->json([
            'code' => 302,
            'msg'  => '系统错误，请稍候再试～'
        ]);
    }
}
